package com.ihighest.app.infox.adapter;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.infox.R;

public class RankingTodayAdapter extends BaseAdapter {
	JSONArray array;
	Context context;
	LayoutInflater inflater = null;

	public RankingTodayAdapter(Context activity, JSONArray array) {
		this.context = activity;
		this.array = array;
		inflater = LayoutInflater.from(activity);

	}

	public RankingTodayAdapter(Context activity) {
		this.context = activity;
		inflater = LayoutInflater.from(activity);

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return array == null ? 0 : array.length();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@SuppressWarnings("deprecation")
	@Override
	public View getView(int position, View contentView, ViewGroup parent) {
		ViewHolder holder;
		if (contentView == null) {
			holder = new ViewHolder();
			contentView = inflater.inflate(R.layout.infox_activity_rankingtoday_item, null);
			holder.introduction = (TextView) contentView.findViewById(R.id.rankingToday_introduction);
			holder.name = (TextView) contentView.findViewById(R.id.rankingToday_name);
			holder.rankingImg = (ImageView) contentView.findViewById(R.id.rankingToday_img);
			holder.ranking = (TextView) contentView.findViewById(R.id.rankingToday_rank);
			contentView.setTag(holder);
		} else {
			holder = (ViewHolder) contentView.getTag();
		}
		if (position == 0) {
			holder.ranking.setBackgroundResource(R.drawable.infox_searchitem_first);
		} else if (position == 1) {
			holder.ranking.setBackgroundResource(R.drawable.infox_searchitem_second);
		} else if (position == 2) {
			holder.ranking.setBackgroundResource(R.drawable.infox_searchitem_third);
		} else {
			holder.ranking.setVisibility(View.GONE);
		}
		// Drawable drawable =
		// context.getResources().getDrawable(R.drawable.infox_img_loadrong);
		// DisplayImageOptions defaultOptions = new
		// DisplayImageOptions.Builder().showImageForEmptyUri(drawable).showImageOnFail(drawable)
		// .showImageOnLoading(drawable).cacheInMemory(true).cacheOnDisc(true).build();
		try {
			AppKit.SetNormalImg(array.getJSONObject(position).getString("image"), holder.rankingImg);
			// ImageLoader.getInstance().displayImage(array.getJSONObject(position).getString("image"),
			// holder.rankingImg, defaultOptions);
			holder.introduction.setText(array.getJSONObject(position).getString("memo"));
			holder.name.setText(array.getJSONObject(position).getString("title"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return contentView;
	}

	class ViewHolder {
		TextView ranking;// 排行
		ImageView rankingImg;// 图标
		TextView name;
		TextView introduction;// 介绍
	}

	/**
	 * 替换当前数据源数据
	 * 
	 * @param moreArry
	 * 
	 * @author lkx Created at 2015-10-20 下午8:49:23
	 */
	public void setData(JSONArray jsonArray) {
		array = jsonArray;
		notifyDataSetChanged();
	}

	/**
	 * 向已有数据源中追加数据
	 * 
	 * @param jsonArray
	 * 
	 * @author lkx Created at 2015-10-20 下午8:49:00
	 */
	public void appendData(JSONArray jsonArray) {
		try {
			array = AppKit.joinJSONArray(array, jsonArray);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		notifyDataSetChanged();
	}
}
